package com.lxt.controller;

import com.lxt.common.HttpResult;
import com.lxt.common.util.RedisOperator;
import com.lxt.pojo.Orders;
import com.lxt.pojo.Users;
import com.lxt.pojo.VO.UsersVO;
import com.lxt.service.center.MyOrdersService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import java.io.File;
import java.util.UUID;

@Controller
public class BaseController {

    @Autowired
    private MyOrdersService myOrdersService;
    @Autowired
    private RedisOperator redisOperator;

    /**
     * 初始化页面相关参数
     */
    public static final Integer COMMENT_PAGE = 1;
    public static final Integer COMMENT_PAGE_SIZE = 10;
    public static final Integer PAGE_SIZE = 20;

    public static final String FOODIE_SHOP_CART = "shopcart";
    public static final String REDIS_USER_TOKEN = "redis_user_token";

    //支付中心的调用地址
    public static final String paymentUrl = "http://payment.t.mukewang.com/foodie-payment/payment/createMerchantOrder";

    // 微信支付成功 ->支付中心 -> 天天吃货平台 (回调通知的url)
    // 此处回调接口需要使用内网穿透将服务在公网上可访问
    public static final String payReturnUrl = "http://localhost:8088/orders/notifyMerchantOrderPaid";

    // 用户上传头像的位置(File.separator可以兼容多种操作系统)
    public static final String IMAGE_USER_FACE_LOCATION = File.separator + "IDEA_Code" +
            File.separator + "architect" + File.separator + "foodie-center" + File.separator + "face";
    //public static final String IMAGE_USER_FACE_LOCATION = "/IDEA_Code/architect/foodie-center/face";


    /**
     * 用于验证用户和订单是否有关联关系，避免非法用户调用
     *
     * @return
     */
    public HttpResult checkUserOrder(String orderId, String userId) {
        Orders order = myOrdersService.queryMyOrder(orderId, userId);
        if (order == null) {
            HttpResult.errorMsg("订单不存在!");
        }
        return HttpResult.ok(order);
    }

    public UsersVO conventUsersVO(Users user) {
        // 实现用户的redis会话
        String uniqueToken = UUID.randomUUID().toString().trim();
        redisOperator.set(REDIS_USER_TOKEN + ":" + user.getId(), uniqueToken);

        UsersVO usersVO = new UsersVO();
        BeanUtils.copyProperties(user, usersVO);
        usersVO.setUserUniqueToken(uniqueToken);

        return usersVO;
    }
}
